from flask import jsonify


class ApiResponse:
    """统一API响应格式"""

    @staticmethod
    def success(data=None, msg="操作成功", code=200):
        """成功响应"""
        return jsonify({
            'code': code,
            'msg': msg,
            'success': True,
            'data': data
        })

    @staticmethod
    def error(msg="操作失败", code=400, data=None):
        """错误响应"""
        return jsonify({
            'code': code,
            'msg': msg,
            'success': False,
            'data': data
        }), code

    @staticmethod
    def unauthorized(msg="未授权访问", code=401):
        """未授权响应"""
        return ApiResponse.error(msg, code)

    @staticmethod
    def forbidden(msg="禁止访问", code=403):
        """禁止访问响应"""
        return ApiResponse.error(msg, code)

    @staticmethod
    def not_found(msg="资源不存在", code=404):
        """未找到响应"""
        return ApiResponse.error(msg, code)

    @staticmethod
    def server_error(msg="服务器内部错误", code=500):
        """服务器错误响应"""
        return ApiResponse.error(msg, code)